/**
 * @description: 定义班级 service
 * @version: 0.0.1
 * @author: z..
 * @Date: 2023-06-12 18:16:39
 */

import { Provide } from '@midwayjs/core';
import { UserRoleContact } from './user_role_contact.entity';

import { InjectRepository, InjectDataSource } from '@midwayjs/sequelize';
import { Repository, Sequelize } from 'sequelize-typescript';

@Provide()
export class UserRoleContactModel {

  @InjectRepository(UserRoleContact)
  UserRoleContactRepository: Repository<UserRoleContact>;

  @InjectDataSource()
  SequelizeQuery: Sequelize;

  /**
   * 新增用户角色信息
   * @returns 
   */
  async created(userId: number, roleId: number): Promise<any> {
    return await this.UserRoleContactRepository.create(
      {
        role_id: roleId,
        user_id: userId
      }
    );
  };

  /**
   * 更新用户角色信息
   * @returns 
   */
  async update(userId: number, roleId: number): Promise<any> {
    return await this.UserRoleContactRepository.update({
      role_id: roleId
    },
      {
        where: {
          user_id: userId
        }
      }
    );
  };

  /**
   * 查询当前用户角色信息
   * @param userId 
   * @param roleId 
   * @returns 
   */
  async getNum(userId: number): Promise<any> {
    return await this.UserRoleContactRepository.count(
      {
        where: {
          user_id: userId
        }
      }
    );
  };

}